import collections
class Solution:
    def Tjon(self, n, edges):
        low = [-1] * n
        time = [-1] * n
        stack = []
        self.index = 0
        d = collections.defaultdict(set)
        for u, v in edges:
            d[u].add(v)
        st = []
        def dfs(node):
            if time[node] == -1:
                stack.append(node)
                time[node] = self.index
                low[node] = time[node]
                self.index += 1
                for v in d[node]:
                    low[node] = min(low[node], dfs(v))
                if low[node] == time[node]:
                    now_circle = []
                    while True:
                        q = stack.pop()
                        now_circle.append(q)
                        if q == node:
                            st.append(now_circle)
                            break
                return low[node]
            else:
                return low[node]
        ## 确保每个点都能被遍历到
        for i in range(n):
            dfs(i)
        return st


data = Solution()
edges = [[0, 1], [1, 2], [1, 4], [1, 3], [2, 4], [2, 6], [6, 4], [4, 5], [5, 6], [3, 0], [3, 4]]
n = 7
print(data.Tjon(n, edges))
